package com.lengmou.ctrl;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.lengmou.entity.RechargeCards;
import com.lengmou.service.RechargeCardsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Cards")
public class RechargeCardsContoller {

    @Resource
    private RechargeCardsService rechargeCardsService;


    @RequestMapping(value = "/getAll",produces = "application/json; charset=utf-8")
    @ResponseBody
   public String getAll(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                        @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                        String id,Integer state,Integer userid,String createtime,String usetime,Double minPrice,Double maxPrice
                        ){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("state",state);
        map.put("userid",userid);
        map.put("createtime",createtime);
        map.put("usetime",usetime);
        map.put("minPrice",minPrice);
        map.put("maxPrice",maxPrice);
        JSONObject json = new JSONObject();
        PageHelper.startPage(pageNum,pageSize);
        List<RechargeCards> search = rechargeCardsService.search(map);
        json.put("code",0);
        json.put("data",search);
        json.put("count",rechargeCardsService.search(map).size());
        return json.toJSONString();
   }

    @RequestMapping(value = "/setMoney",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setMoney(RechargeCards cards){
        JSONObject json = new JSONObject();
        if(cards!=null){
            int i = rechargeCardsService.setCarsMoney(cards);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/setState",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setState(RechargeCards cards){
        JSONObject json = new JSONObject();
        if(cards!=null){
            int i = rechargeCardsService.setCarsState(cards);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/setCreateTime",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setCreateTime(RechargeCards cards){
        JSONObject json = new JSONObject();
        if(cards!=null){
            int i = rechargeCardsService.setCardsCreateTime(cards);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/setUseTime",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setUseTime(RechargeCards cards){
        JSONObject json = new JSONObject();
        if(cards!=null){
            int i = rechargeCardsService.setCarsUseTime(cards);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/setUserid",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String setUserid(RechargeCards cards){
        JSONObject json = new JSONObject();
        if(cards!=null){
            int i = rechargeCardsService.setCarsUserid(cards);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }

    @RequestMapping(value = "/addCards",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String addCards(Integer num,Double money){
        JSONObject json = new JSONObject();
        if(money>0){
            int i = rechargeCardsService.addsCars(num,money);
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }
    @RequestMapping(value = "/jsonCards",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String jsonCards(){
        JSONObject json = new JSONObject();
        List<RechargeCards> all = rechargeCardsService.getAll();
        json.put("cards",all);
        return json.toString();
    }
    @RequestMapping(value = "/deleteAll",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String deleteAll( @RequestBody List<RechargeCards> cars){
        JSONObject json = new JSONObject();
        if(cars!=null&&cars.size()>0){
            int i = 0;
            for (RechargeCards o:cars) {
                i+=rechargeCardsService.deleteCars(o.getId());
            }
            json.put("state",i);
        }else{
            json.put("state",-1);
        }
        return json.toString();
    }



}
